package com.bsh.api;

import javax.servlet.http.HttpServletRequest;

import com.bsh.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiCloudService;
import com.bsh.utils.ApiMessage;

/**
 * 商币相关
 * 
 * @author dl
 *
 */
@Controller
@RequestMapping(value = "/api/v1/cloud", method = { RequestMethod.POST })
public class ApiCloudController {

	@Autowired
	HttpServletRequest request;
	@Autowired
	ApiCloudService apiCloudService;

	/**
	 * 商币赠送
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/sendCloudMoney", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage sendCloudMoney(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCloudService.sendCloudMoney(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 分享增加商币
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addCloudMoney", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage addCloudMoney(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCloudService.addCloudMoney(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 余额购买商币
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/buyCloudMoney", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage buyCloudMoney(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCloudService.buyCloudMoney(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}
	
	
	
	/**
	 * 是否能购买商币
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/isBuyCloudMoney", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage isBuyCloudMoney(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCloudService.isBuyCloudMoney(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}
	
	/**
	 * 能否商币兑换期权
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/isCloudToShares", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage isCloudToShares(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCloudService.isCloudToShares(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}
	
	/**
	 * 商币兑换期权
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/cloudToShares", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage cloudToShares(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCloudService.cloudToShares(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}
}
